package cq.zhcqk.fragment._4_sq;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.loopj.android.http.AsyncHttpClient;

import cq.zhcqk.R;
import cq.zhcqk.activity.AXST_ExplainActivity;
import cq.zhcqk.activity.AXST_MainActivity;
import cq.zhcqk.activity.GzyyDetailActivity;
import cq.zhcqk.activity.SQ_FW_WaterActivity;
import cq.zhcqk.activity.SQ_JtysActivity;
import cq.zhcqk.activity.SQ_SqjjActivity;
import cq.zhcqk.activity.SQ_SwzlActivity;
import cq.zhcqk.activity.ZW_ListActivity;
import cq.zhcqk.axst.fragment.AxstInfo;
import cq.zhcqk.util.AsyncHttpClientUtils;

/**
 * Created by zhangle on 2017/12/3.
 */
public class _4_SqfwFragment extends Fragment implements View.OnClickListener{
    private View view;
    private Context context;
    private AsyncHttpClient httpClient;

    private TextView jj1;
    private TextView jj2;
    private TextView jj3;
    private TextView jj4;

    private TextView swzl;
    private TextView jtys;

    private TextView fw_water;
    private TextView fw_electric;
    private TextView fw_gas;
    private TextView fw_tv;

    private String url = "alipays://platformapi/startapp?appId=20000193&url=/www/setNewAccount.htm?subBizType=";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if(view==null){
            view = inflater.inflate(R.layout.fragment_sqfw_4_4, container, false);
            httpClient = AsyncHttpClientUtils.getInstance().getAsyncHttpClient();
            context = getActivity();
            initView();
        }
        ViewGroup parent=(ViewGroup)view.getParent();
        if(parent!=null){
            parent.removeView(view);
        }
        return view;
    }

    private void initView(){
        fw_water = (TextView) view.findViewById(R.id.fw_water);
        fw_electric = (TextView) view.findViewById(R.id.fw_electric);
        fw_gas = (TextView) view.findViewById(R.id.fw_gas);
        fw_tv = (TextView) view.findViewById(R.id.fw_tv);

        jj1 = (TextView) view.findViewById(R.id.jj1);
        jj2 = (TextView) view.findViewById(R.id.jj2);
        jj3 = (TextView) view.findViewById(R.id.jj3);
        jj4 = (TextView) view.findViewById(R.id.jj4);

        swzl = (TextView) view.findViewById(R.id.swzl);
        jtys = (TextView) view.findViewById(R.id.jtys);

        jj1.setOnClickListener(this);
        jj2.setOnClickListener(this);
        jj3.setOnClickListener(this);
        jj4.setOnClickListener(this);

        swzl.setOnClickListener(this);
        jtys.setOnClickListener(this);

        fw_water.setOnClickListener(this);
        fw_electric.setOnClickListener(this);
        fw_gas.setOnClickListener(this);
        fw_tv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()){
            case R.id.jj1:
                intent = new Intent(context, ZW_ListActivity.class);
//                intent = new Intent(context, SQ_SqjjActivity.class);
                intent.putExtra("type","18");
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                break;
            case R.id.jj2:
                intent = new Intent(context, ZW_ListActivity.class);
//                intent = new Intent(context, SQ_SqjjActivity.class);
                intent.putExtra("type","19");
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                break;
            case R.id.jj3:
                intent = new Intent(context, ZW_ListActivity.class);
//                intent = new Intent(context, SQ_SqjjActivity.class);
                intent.putExtra("type","20");
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                break;
            case R.id.jj4:
                intent = new Intent(context, ZW_ListActivity.class);
//                intent = new Intent(context, SQ_SqjjActivity.class);
                intent.putExtra("type","21");
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                break;

            case R.id.swzl:
//                intent = new Intent(context, SQ_SwzlActivity.class);
//                startActivity(intent);
//                getActivity().overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                intent = new Intent(context,AXST_ExplainActivity.class);
//                intent = new Intent(context,AXST_MainActivity.class);
//                intent = new Intent(context,AxstInfo.class);
                intent.putExtra("cid","146");
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                break;
            case R.id.jtys:
                intent = new Intent(context, SQ_JtysActivity.class);
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                break;

            case R.id.fw_water:
                startAlipay("WATER");
                break;
            case R.id.fw_electric:
                startAlipay("ELECTRIC");
                break;
            case R.id.fw_gas:
                startAlipay("GAS");
                break;
            case R.id.fw_tv:
                startAlipay("CATV");
                break;
        }
    }

    private void startAlipay(String type){
        boolean visit = checkAliPayInstalled(context);
        Intent intent = null;
        if(visit){
            intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url+type));
            startActivity(intent);
        }
    }

    //判断是否安装支付宝app
    public static boolean checkAliPayInstalled(Context context) {
        Uri uri = Uri.parse("alipays://platformapi/startApp");
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        ComponentName componentName = intent.resolveActivity(context.getPackageManager());
        return componentName != null;
    }
}
